import CryptoJS from 'crypto-js'
import MD5 from 'crypto-js/md5'
import { cookie } from 'vux'

let k=cookie.get('key');
let v=cookie.get('iv');
let key = CryptoJS.enc.Utf8.parse(k);
let iv = CryptoJS.enc.Utf8.parse(v);
// 加密
const Encrypt = function (word) {
    srcs = CryptoJS.enc.Utf8.parse(word);
    let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7});
    return encrypted.ciphertext.toString().toUpperCase();
}

// 解密
const Decrypt = function (word) {
    var encryptedHexStr = CryptoJS.enc.Base64.parse(word);
    var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    var decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7});
    var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
}

const getmd5=function(word){
    var md5 = CryptoJS.MD5(word).toString();
    return md5;
}

export{Encrypt,Decrypt,getmd5}
